import { Component, OnInit } from '@angular/core';
import { EventEmitService } from '../../service/EventEmitService';
import { TutorialService } from "../../service/tutorialService";
import { ThrowStmt } from '@angular/compiler';
import { ActivatedRoute } from '@angular/router';
import { MarkdownFile } from 'src/app/model/MarkdownFile';

@Component({
  selector: 'app-mdrender',
  templateUrl: './mdrender.component.html',
  styleUrls: ['./mdrender.component.css']
})
export class MdrenderComponent implements OnInit {

  tutorialId:number;
  currentMdFile:MarkdownFile;
  title:string;
  author:string;
  readcount:number;
  text:string;
  date:string;
  isEmptyFile:boolean = true;
  constructor(private eventEmitService:EventEmitService,
              private tutorialService:TutorialService,
              private route: ActivatedRoute) { }

  ngOnInit(): void {
    this.eventEmitService.markdownfileEventer.subscribe(id => {
      if(id == 0){
        this.isEmptyFile = true;
      }
      else{
        this.isEmptyFile = false;
        this.getMdFile(id);
      }

    })
    this.tutorialId =  this.route.snapshot.queryParams["id"];
    if(this.tutorialId != null){
      this.isEmptyFile = false;
      this.getMdFile(this.tutorialId);
    }
  }

  getMdFile(id:number){
    this.tutorialService.getMarkdownFile(id).subscribe(mdfile=>{
      console.log("get markdown file");
      console.log(mdfile);
      this.currentMdFile = mdfile;
      if(mdfile.text == null) this.text = "";
      else this.text = mdfile.text;
      this.title = mdfile.filename;
      this.author = mdfile.author;
      this.date = mdfile.date;
    });
  }

}
